from django.apps import AppConfig
from django.db import connection
import logging

logger = logging.getLogger('django')

class IotYkcSimulatorConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'iot_ykc_simulator'
    verbose_name = "云快充充电模拟器"  # 设置应用显示名

    def ready(self):
        # 导入信号处理
        import iot_ykc_simulator.signals
        if not hasattr(self, '_reset_done'):
            try:
                # 仅检查表是否存在，避免迁移错误
                if "iot_ykc_simulator_pile" in connection.introspection.table_names():
                    from .models import Pile
                    Pile.objects.filter(logged_in=True).update(logged_in=False)
                    #logger.info("✅ 重置所有桩登录状态为未登录")
            except Exception as e:
                logger.error(f"⚠️ 重置错误: {e}")
            self._reset_done = True